/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */

package com.fuyao.cloud.space.roomland.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.common.core.util.KeyValue;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.data.mybatis.SpliceQueryWrapper;
import com.fuyao.cloud.common.log.annotation.SysLog;
import com.fuyao.cloud.space.dto.SearchRoomLandDTO;
import com.fuyao.cloud.space.entity.RoomLand;
import com.fuyao.cloud.space.roomland.service.RoomLandService;
import com.fuyao.cloud.space.vo.RoomLandExcelVo;
import org.springframework.security.access.prepost.PreAuthorize;
import com.fuyao.cloud.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 房屋模块
 *
 * @author 时间的朋友
 * @date 2021-11-09 15:19:15
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/fyroomland" )
@Api(value = "fyroomland", tags = "房屋模块管理")
public class RoomLandController {

    private final RoomLandService fyRoomLandService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param fyRoomLand 房屋模块
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('roomland_fyroomland_view')" )
    public R getFyRoomLandPage(Page page, SearchRoomLandDTO fyRoomLand) {
        return R.ok(fyRoomLandService.page(page, SpliceQueryWrapper.spliceSearchCondition(fyRoomLand)));
    }

    /**
     * 通过id查询房屋模块
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('roomland_fyroomland_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(fyRoomLandService.getById(id));
    }

    /**
     * 通过code查询
     *
     * @param code 土地编号
     * @return 土地对象
     */
    @ApiOperation(value = "通过code查询", notes = "通过code查询")
    @GetMapping("/getCodeObj/{code}" )
    public R getCodeObj(@PathVariable("code" )String code) {
        return R.ok(fyRoomLandService.list(Wrappers.<RoomLand>lambdaQuery().eq(RoomLand::getLandNumber, code)));
    }

    /**
     * 新增房屋模块
     * @param fyRoomLand 房屋模块
     * @return R
     */
    @ApiOperation(value = "新增房屋模块", notes = "新增房屋模块")
    @SysLog("新增房屋模块" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('roomland_fyroomland_add')" )
    public R save(@RequestBody RoomLand fyRoomLand) {
        fyRoomLandService.save(fyRoomLand);
        return R.ok(fyRoomLand);
    }

    /**
     * 修改房屋模块
     * @param fyRoomLand 房屋模块
     * @return R
     */
    @ApiOperation(value = "修改房屋模块", notes = "修改房屋模块")
    @SysLog("修改房屋模块" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('roomland_fyroomland_edit')" )
    public R updateById(@RequestBody RoomLand fyRoomLand) {
        return R.ok(fyRoomLandService.updateById(fyRoomLand));
    }

    /**
     * 通过id删除房屋模块
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除房屋模块", notes = "通过id删除房屋模块")
    @SysLog("通过id删除房屋模块" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('roomland_fyroomland_del')" )
    public R removeById(@PathVariable Long id) {
        return fyRoomLandService.batchDelete(Collections.singletonList(id));
    }

    /**
     * 通过id删除房屋模块
     * @param ids ids
     * @return R
     */
    @ApiOperation(value = "通过id删除房屋模块", notes = "通过id删除房屋模块")
    @SysLog("通过id删除房屋模块" )
    @DeleteMapping("/batchDelete" )
    @PreAuthorize("@pms.hasPermission('roomland_fyroomland_del')" )
    public R batchDelete(@RequestBody List<Long> ids) {
        return fyRoomLandService.batchDelete(ids);
    }

    /**
     * 导出excel 表格
     * @param fyRoomLand 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('roomland_fyroomland_export')" )
    public List<RoomLandExcelVo> export(SearchRoomLandDTO fyRoomLand) {
    	return fyRoomLandService.listRoomLandExcel(SpliceQueryWrapper.spliceSearchCondition(fyRoomLand));
    }

	/**
	 * 获取土地房屋信息中所有id和资产名称
	 * @return R
	 */
	@ApiOperation(value = "获取土地房屋信息中所有id和资产名称", notes = "获取土地房屋信息中所有id和资产名称")
	@GetMapping("/IdAndAssertName" )
	public List<KeyValue<Long,String>> getIdAndAssertName() {
		return this.fyRoomLandService.listIdAndAssetName();
	}

}
